package com.base.app.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import cn.mysteel.util.ObjectUtils;

import com.base.app.dao.ArticleDao;
import com.base.app.dao.ArticlePictureDao;
import com.base.app.dao.ChannelDao;
import com.base.app.entity.Channel;
import com.base.data.exception.DataException;
import com.mysteel.page.Page;

@Service("channelService")
public class ChannelServiceImpl implements ChannelService
{
	@Autowired
	private ChannelDao channelDao;
	@Autowired
	private ArticleDao articleDao;
	@Autowired
	private ArticlePictureDao articlePictureDao;
	
	
	@Override
	public void saveChannel(Channel channel)
	{
		Assert.notNull (channel);
		
		Map<String,Object> params = new HashMap<String,Object>();
		params.put ("name" ,channel.getName ());
		
		List<Long> list = channelDao.query (params);
		
		if(ObjectUtils.notEmpty (list))
		{
			throw new DataException("该频道已经存在");
		}
		
		channelDao.save (channel);
	}
	
	@Override
	public void deleteChannel(long id)
	{
		channelDao.delete(id);
		
		Map<String,Object> params = new HashMap<String,Object>();
		
		params.put ("channelId" ,id);
		List<Long> list = articleDao.query (params);
		
		if(ObjectUtils.notEmpty (list))
		{
			for(long ids:list)
			{
				articleDao.delete (ids);
				
				Map<String,Object> params2 = new HashMap<String,Object>();
				
				params2.put ("articleId" ,ids);
				
				List<Long> list2 = articlePictureDao.query (params2);
				
				if(ObjectUtils.notEmpty (list2))
				{
					for(long id2 : list2)
					{
						articlePictureDao.delete (id2);
					}
				}
			}
		}
	}
	
	@Override
	public void updateChannel(Channel channel)
	{
		Assert.notNull (channel);
		
		Channel ch = channelDao.get (channel.getId ());
		if(ObjectUtils.notNull (ch))
		{
			ch.setName (channel.getName ());
			ch.setCount (channel.getCount());
			ch.setStatus (channel.isStatus ());
			ch.setAdminId (channel.getAdminId ());
			ch.setAdminName (channel.getAdminName ());
			ch.setLastAccess (channel.getLastAccess ());
			ch.setLastAccessName (channel.getLastAccessName ());
			ch.setDescription (channel.getDescription ());
			
			channelDao.update (ch);
		}
	}
	
	@Override
	public Channel getChannelById(long id)
	{	
		return channelDao.get (id);
	}
	
	@Override
	public Page<Channel> queryChannel(Map<String, Object> params,int currentPageNum, int maxPageRowCount)
	{
		Page<Channel> page = null;

		if (ObjectUtils.isNull(params))
		{
			params = new HashMap<String, Object>(2);
		}
		
		int count = channelDao.count(params);

		if (count > 0)
		{
			page = new Page<Channel>();
			page.setTotalRowCount(count);
			page.setCurrentPageNum(currentPageNum);
			page.setMaxPageRowCount(maxPageRowCount);
			page.setURL(params);

			params.put("startPageRowCount", page.getStartPageRowCount());
			params.put("maxPageRowCount", page.getMaxPageRowCount());

			List<Long> chIds = channelDao.query(params);

			if (ObjectUtils.notEmpty(chIds))
			{
				List<Channel> channels = new ArrayList<Channel>(chIds.size());

				for (long id : chIds)
				{
					channels.add(getChannelById (id));
				}

				page.setDatas(channels);
			}
		}

		return page;
	}
	
}
